package etr.bk.gui.rpan.info;

import etr.bk.gui.*;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerDateModel;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;

public class TextInfoPanel extends JPanel
{
	private JTextField nameEd;
	private JTextField surNameEd;
	private JTextField birdayEd;

	private JRadioButton male;
	private JRadioButton female;
	
	private ButtonGroup groupSex;

	private JTextField countryEd;
	private JTextField cityEd;
	private JTextField addrEd;
	private JTextField workAdrEd;
	private JTextField mailAdrEd;
	private JTextField phoneNumbEd;
	private JTextField isqNumbEd;
	private JTextField skypNumbEd;

	ArrayList<JTextField> itemList;
	
	MainListener mListener;

	public TextInfoPanel()
	{
		super();
		
		mListener = MainListener.getListener();

		this.setBorder(BorderFactory.createRaisedBevelBorder());

		FlowLayout layout = new FlowLayout(FlowLayout.LEFT);
		this.setLayout(layout);

		JLabel name = new JLabel("Name: ");
		this.nameEd = new JTextField(9);
		mListener.setTextFieldName(nameEd);
		this.nameEd.getDocument().addDocumentListener(mListener);
		JLabel surName = new JLabel("SurName: ");
		this.surNameEd = new JTextField(10);
		mListener.setTextFieldSurname(surNameEd);
		this.surNameEd.getDocument().addDocumentListener(mListener);

		JLabel rasp = new JLabel(
				"                                                                                                ");

		// ----------------------------------------------
		JLabel birday = new JLabel("Birthday:");
		// SpinnerDateModel spiModel = new SpinnerDateModel();
		// JSpinner birdayEd = new JSpinner(spiModel);
		this.birdayEd = new JTextField(9);
		JLabel strut = new JLabel("     ");
		// -------------Radio butt---------------------------
		this.male = new JRadioButton("Male");
		this.female = new JRadioButton("Female    ");
		groupSex = new ButtonGroup();
		groupSex.add(male);
		groupSex.add(female);

		male.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent arg0){
				System.out.println("-" + TextInfoPanel.this.addrEd.getText()
						+ "-" + TextInfoPanel.this.male.getText());
			}
		});

		// --------------------------------------------------
		JLabel country = new JLabel("Country: ");
		this.countryEd = new JTextField(9);

		JLabel city = new JLabel("City: ");
		this.cityEd = new JTextField(11);

		JLabel addr = new JLabel("Address: ");
		this.addrEd = new JTextField(24);

		JLabel workAdr = new JLabel("Work:");
		this.workAdrEd = new JTextField(26);
		JLabel mailAdr = new JLabel("Mail:");
		this.mailAdrEd = new JTextField(12);

		mailAdrEd.setText("rao.death@gmail.com");
		//mailAdrEd.setEnabled(false);
		mailAdrEd.setDisabledTextColor(Color.BLACK);

		JLabel phoneNumb = new JLabel("Phone:");
		this.phoneNumbEd = new JTextField(9);
		JLabel isqNumb = new JLabel("ISQ:");
		this.isqNumbEd = new JTextField(7);
		JLabel skypNumb = new JLabel("Skype: ");
		this.skypNumbEd = new JTextField(14);

		nameEd.setBorder(BorderFactory.createLoweredBevelBorder());
		surNameEd.setBorder(BorderFactory.createLoweredBevelBorder());

		this.itemList = new ArrayList<JTextField>();
		this.itemList.add(nameEd);
		this.itemList.add(surNameEd);
		this.itemList.add(birdayEd);
		this.itemList.add(countryEd);
		this.itemList.add(cityEd);
		this.itemList.add(addrEd);
		this.itemList.add(workAdrEd);
		this.itemList.add(mailAdrEd);
		this.itemList.add(phoneNumbEd);
		this.itemList.add(isqNumbEd);
		this.itemList.add(skypNumbEd);

		this.add(name);
		this.add(nameEd);
		this.add(surName);
		this.add(surNameEd);

		this.add(rasp);

		this.add(birday);
		this.add(birdayEd);

		this.add(strut);

		this.add(male);
		this.add(female);
		this.add(country);
		this.add(countryEd);
		this.add(city);
		this.add(cityEd);
		this.add(addr);
		this.add(addrEd);
		this.add(workAdr);
		this.add(workAdrEd);
		this.add(mailAdr);
		this.add(mailAdrEd);
		this.add(phoneNumb);
		this.add(phoneNumbEd);
		this.add(isqNumb);
		this.add(isqNumbEd);
		this.add(skypNumb);
		this.add(skypNumbEd);
		
		for (JTextField tmp : this.itemList)
		{
			tmp.setDisabledTextColor(Color.BLACK);
		}
		
		

	}

	public ArrayList<String> getInfo()
	{
		if (this.isMandatoryFieldFill())
		{
			ArrayList<String> infArray = new ArrayList<String>();
			
			String strTmp;
			
			for (JTextField tmp : this.itemList)
			{
				strTmp = tmp.getText();
				if (strTmp != "")
					infArray.add(strTmp);
				else 
					infArray.add("");
			}

			// ---------Special for SEX-------------

			if (this.male.isSelected())
				infArray.add("male");
			else
				infArray.add("female");

			return infArray;
		}
		else
			return null;
	}

	public void setInfo(ArrayList<String> infArray)
	{
		Iterator<String> infIterator = infArray.iterator();
		String str;
		
		for (JTextField tmp : this.itemList)
		{
			str = infIterator.next();
			tmp.setText(str);
		}
		// ---------Special for SEX-------------
		str = infIterator.next();
		if (str.equals("male"))
		{
			this.male.setSelected(true);
		}
		else
		{
			this.female.setSelected(true);
		}

	}

	public boolean isMandatoryFieldFill()
	{
		String empty = "";
		String name = this.nameEd.getText();
		String surn = this.surNameEd.getText();

		if ((name.equals(empty))
				|| (surn.equals(empty))
				|| (this.male.isSelected() == false && this.female.isSelected() == false))
		{
			System.out.println("Ne zapolneni mandatoru fields(name & surname)");
			return false;
		}

		return true;
	}
	
	public void clearAllFields()
	{
		for (JTextField tmp : this.itemList)
		{
			tmp.setText("");
		}
		
		this.groupSex.clearSelection();
	}
	
	public void setAllFieldsInactive()
	{
		for (JTextField tmp : this.itemList)
		{
			tmp.setEnabled(false);
		}
		
		this.male.setEnabled(false);
		this.female.setEnabled(false);
	}
	
	public void setAllFieldsActive()
	{
		for (JTextField tmp : this.itemList)
		{
			tmp.setEnabled(true);
		}
		
		this.male.setEnabled(true);
		this.female.setEnabled(true);
		
	}
	
}
